package com.sinog.biz.schedule;

import com.sinog.biz.BizApplication;
import com.sinog.biz.config.RedisConfig;
import com.sinog.biz.mapper.havexml.PrisonerMainMapper;
import com.sinog.biz.util.CodeUrlUtils;
import com.sinog.biz.util.JdbcUtils;
import com.sinog.core.utilbean.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author wangxj
 * @desc 跨省数据处理功能,例如:北京发往山东的跨省数据,北京将服刑人员已经删除,山东也应该删除此服刑人员,如果山东未将此服刑人员删除,需使用此功能来做相应的数据补偿
 * @date 2022-12-01 14:23
 */
@Slf4j
@Component
@ConditionalOnProperty(name = "gkzx.azbj.inter.open", havingValue = "true")
public class InterprovincialSchedule {

    @Resource
    private PrisonerMainMapper prisonerMainMapper;
    @Resource
    private RedisConfig redisConfig;
    private JdbcTemplate middleJdbcTemplate;

    /**
     * 定时任务,每晚上执行
     * 实现步骤
     * 1.使用缓存将跨省数据的priNum进行缓存
     * 例:in:110000 out:120000 priNum:222,333,444
     * out:130000 priNum:555,666,777
     * 2.localCode省份执行定时任务,将属于本省的数据查询出来进行比对
     * (1):本省有外省没有 ---> 删除
     * (2):本省没有外省有 ---> 添加
     */
    @Scheduled(cron = "${gkzx.azbj.inter.cron}")
    private void getLocalProvinceDataFromMiddle() {
        String localCode = CodeUrlUtils.getLocalCode();
        if(!ObjectUtils.allNotNull(middleJdbcTemplate)) {
            middleJdbcTemplate = (JdbcTemplate)BizApplication.applicationContext.getBean("middleJdbcTemplate");
        }
        // 查询出本省数据库中外省所有的服刑人员编号 --> 由外省向本省发送的跨省数据
        List<Map<String,String>> priNums = prisonerMainMapper.selectPrivinceInList(localCode);
        // 连接司法部redis源信息
        try(Jedis jedis = redisConfig.redisPoolFactory().getResource()) {
            try {
                // 遍历循环放入缓存中
                for(Map<String,String> map : priNums) {
                    String province = map.get("province");
                    String priNums1 = map.get("priNums");
                    jedis.hset("inter_" + localCode,province,priNums1);
                }
                // inter_110000,inter_370000
                // 111,222       222,333
                // 从缓存中抽取外省所有的priNums
                Set<String> provinces = jedis.keys("inter_*");
                for(String key : provinces) {
                    if(!key.contains(localCode)) {
                        // 通过字符串截取获得省份code
                        String province = key.substring(6);
                        // 查询本省数据库中在外省服刑的人员编号 --> 由本省录入发向外省的跨省数据
                        List<String> outList = prisonerMainMapper.selectPrivinceOutList(province,localCode);
                        // 获取province中本省的数据开始操作
                        String allPriNum = jedis.hget(key,localCode);
                        // 外省数据库中存在本省priNum的集合
                        List<String> splitPriNum = Arrays.asList(allPriNum.split(","));
                        for(String priNum : splitPriNum) {
                            // 由本省发起跨省数据,本省不存在,外省存在则执行删除外省操作
                            if(!outList.contains(priNum)) {
                                // 执行外省删除操作
                                toDeleteInfo(priNum,localCode,province);
                            }
                        }
                        // 本省存在数据,外省不存在数据则进行添加操作
                        for(String value : outList) {
                            if(!splitPriNum.contains(value)) {
                                // 执行外省添加操作
                                toSaveInfo(value,localCode,province);
                            }
                        }
                    }
                }
            } catch(Exception e) {
                log.error(e.getMessage());
            } finally {
                jedis.close();
            }
        }
    }

    /**
     * 执行删除逻辑
     * @param priNum priNum
     * @param outProvince 谁发的
     * @param inProvince 谁接收
     */
    private void toDeleteInfo(String priNum,String outProvince,String inProvince) {
        String prisonerMainDeleteSql = "delete from prisoner_main where pri_num=\'" + priNum + "\'";
        String prisaveTempDeleteSql = "delete from prisave_temp where pri_num=\'" + priNum + "\'";
        String prisonerFxDeleteSql = "delete from prisoner_fx where pri_num=\'" + priNum + "\'";
        String prisonerHcDeleteSql = "delete from prisoner_hc where pri_num=\'" + priNum + "\'";
        String prisonerSrDeleteSql = "delete from prisoner_sr where pri_num=\'" + priNum + "\'";
        String prisonerZnDeleteSql = "delete from prisoner_zn where pri_num=\'" + priNum + "\'";
        String prisonerZzDeleteSql = "delete from prisoner_zz where pri_num=\'" + priNum + "\'";
        String uploadAppendixFileDeleteSql = "delete from upload_appendix_file where pri_num=\'" + priNum + "\'";
        String xjReceiptDeleteSql = "delete from xj_receipt where pri_num='" + priNum + "'";
        String xjReceiptDataDeleteSql = "delete from xj_receipt_data where session_id in (select xjid from xj_receipt where pri_num ='" + priNum + "')";
        String prisonersJxjsDeleteSql = "delete from prisoners_jxjs where pri_num='" + priNum + "'";
        String prisonersZyjnDeleteSql = "delete from prisoners_zyjn where pri_num='" + priNum + "'";
        String prisonerCjpgDeleteSql = "delete from prisoner_cjpg where pri_num='" + priNum + "'";
        String prisonerXjxxDeleteSql = "delete from prisoner_xjxx where pri_num='" + priNum + "'";
        String middleSql1 = JdbcUtils.createMiddleSql(priNum,prisonerMainDeleteSql,Constants.Number.NUM_ZERO,outProvince,inProvince);
        middleJdbcTemplate.update(middleSql1);
        String middleSql2 = JdbcUtils.createMiddleSql(priNum,prisaveTempDeleteSql,Constants.Number.NUM_ZERO,outProvince,inProvince);
        middleJdbcTemplate.update(middleSql2);
        String middleSql3 = JdbcUtils.createMiddleSql(priNum,prisonerFxDeleteSql,Constants.Number.NUM_ZERO,outProvince,inProvince);
        middleJdbcTemplate.update(middleSql3);
        String middleSql4 = JdbcUtils.createMiddleSql(priNum,prisonerHcDeleteSql,Constants.Number.NUM_ZERO,outProvince,inProvince);
        middleJdbcTemplate.update(middleSql4);
        String middleSql5 = JdbcUtils.createMiddleSql(priNum,prisonerSrDeleteSql,Constants.Number.NUM_ZERO,outProvince,inProvince);
        middleJdbcTemplate.update(middleSql5);
        String middleSql6 = JdbcUtils.createMiddleSql(priNum,prisonerZnDeleteSql,Constants.Number.NUM_ZERO,outProvince,inProvince);
        middleJdbcTemplate.update(middleSql6);
        String middleSql7 = JdbcUtils.createMiddleSql(priNum,prisonerZzDeleteSql,Constants.Number.NUM_ZERO,outProvince,inProvince);
        middleJdbcTemplate.update(middleSql7);
        String middleSql8 = JdbcUtils.createMiddleSql(priNum,uploadAppendixFileDeleteSql,Constants.Number.NUM_ZERO,outProvince,inProvince);
        middleJdbcTemplate.update(middleSql8);
        String middleSql10 = JdbcUtils.createMiddleSql(priNum,prisonersJxjsDeleteSql,Constants.Number.NUM_ZERO,outProvince,inProvince);
        middleJdbcTemplate.update(middleSql10);
        String middleSql11 = JdbcUtils.createMiddleSql(priNum,prisonersZyjnDeleteSql,Constants.Number.NUM_ZERO,outProvince,inProvince);
        middleJdbcTemplate.update(middleSql11);
        String middleSql12 = JdbcUtils.createMiddleSql(priNum,prisonerCjpgDeleteSql,Constants.Number.NUM_ZERO,outProvince,inProvince);
        middleJdbcTemplate.update(middleSql12);
        String middleSql13 = JdbcUtils.createMiddleSql(priNum,prisonerXjxxDeleteSql,Constants.Number.NUM_ZERO,outProvince,inProvince);
        middleJdbcTemplate.update(middleSql13);
        String middleSql14 = JdbcUtils.createMiddleSql(priNum,xjReceiptDeleteSql,Constants.Number.NUM_ZERO,outProvince,inProvince);
        middleJdbcTemplate.update(middleSql14);
        String middleSql15 = JdbcUtils.createMiddleSql(priNum,xjReceiptDataDeleteSql,Constants.Number.NUM_ZERO,outProvince,inProvince);
        middleJdbcTemplate.update(middleSql15);
    }

    /**
     * 执行新增逻辑
     * @param value priNum
     * @param outProvince 谁发的
     * @param inProvince 谁接收
     */
    private void toSaveInfo(String value,String outProvince,String inProvince) {
        List<String> prisonerMainInsertSql = JdbcUtils.getInsertSql("prisoner_main","pri_num",value,null,null,null,null);
        List<String> prisaveTempinsertSql = JdbcUtils.getInsertSql("prisave_temp","pri_num",value,null,null,null,null);
        List<String> prisonerFxinsertSql = JdbcUtils.getInsertSql("prisoner_fx","pri_num",value,null,null,null,null);
        List<String> prisonerHcinsertSql = JdbcUtils.getInsertSql("prisoner_hc","pri_num",value,null,null,null,null);
        List<String> prisonerSrinsertSql = JdbcUtils.getInsertSql("prisoner_sr","pri_num",value,null,null,null,null);
        List<String> prisonerZninsertSql = JdbcUtils.getInsertSql("prisoner_zn","pri_num",value,null,null,null,null);
        List<String> prisonerZzinsertSql = JdbcUtils.getInsertSql("prisoner_zz","pri_num",value,null,null,null,null);
        List<String> uploadAppendixFileInsertSql = JdbcUtils.getInsertSql("upload_appendix_file","pri_num",value,null,null,null,null);
        List<String> prisonersJxjsInsertSql = JdbcUtils.getInsertSql("prisoners_jxjs","pri_num",value,null,null,null,null);
        List<String> prisonersZyjnInsertSql = JdbcUtils.getInsertSql("prisoners_zyjn","pri_num",value,null,null,null,null);
        List<String> prisonerCjpgInsertSql = JdbcUtils.getInsertSql("prisoner_cjpg","pri_num",value,null,null,null,null);
        List<String> prisonerXjxxInsertSql = JdbcUtils.getInsertSql("prisoner_xjxx","pri_num",value,null,null,null,null);
        List<String> xjReceiptInsertSql = JdbcUtils.getInsertSql("xj_receipt","pri_num",value,null,null,null,null);
        List<String> xjReceiptDataInsertSql = JdbcUtils.getInsertSql1("xj_receipt_data","session_id",value,null,null,null,null);
        for(String sql : prisonerMainInsertSql) {
            String isql = JdbcUtils.createMiddleSql(value,sql,Constants.Number.NUM_ZERO,outProvince,inProvince);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : prisaveTempinsertSql) {
            String isql = JdbcUtils.createMiddleSql(value,sql,Constants.Number.NUM_ZERO,outProvince,inProvince);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : prisonerFxinsertSql) {
            String isql = JdbcUtils.createMiddleSql(value,sql,Constants.Number.NUM_ZERO,outProvince,inProvince);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : prisonerHcinsertSql) {
            String isql = JdbcUtils.createMiddleSql(value,sql,Constants.Number.NUM_ZERO,outProvince,inProvince);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : prisonerSrinsertSql) {
            String isql = JdbcUtils.createMiddleSql(value,sql,Constants.Number.NUM_ZERO,outProvince,inProvince);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : prisonerZninsertSql) {
            String isql = JdbcUtils.createMiddleSql(value,sql,Constants.Number.NUM_ZERO,outProvince,inProvince);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : prisonerZzinsertSql) {
            String isql = JdbcUtils.createMiddleSql(value,sql,Constants.Number.NUM_ZERO,outProvince,inProvince);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : uploadAppendixFileInsertSql) {
            String isql = JdbcUtils.createMiddleSql(value,sql,Constants.Number.NUM_ZERO,outProvince,inProvince);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : prisonersJxjsInsertSql) {
            String isql = JdbcUtils.createMiddleSql(value,sql,Constants.Number.NUM_ZERO,outProvince,inProvince);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : prisonersZyjnInsertSql) {
            String isql = JdbcUtils.createMiddleSql(value,sql,Constants.Number.NUM_ZERO,outProvince,inProvince);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : prisonerCjpgInsertSql) {
            String isql = JdbcUtils.createMiddleSql(value,sql,Constants.Number.NUM_ZERO,outProvince,inProvince);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : prisonerXjxxInsertSql) {
            String isql = JdbcUtils.createMiddleSql(value,sql,Constants.Number.NUM_ZERO,outProvince,inProvince);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : xjReceiptInsertSql) {
            String isql = JdbcUtils.createMiddleSql(value,sql,Constants.Number.NUM_ZERO,outProvince,inProvince);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : xjReceiptDataInsertSql) {
            String isql = JdbcUtils.createMiddleSql(value,sql,Constants.Number.NUM_ZERO,outProvince,inProvince);
            middleJdbcTemplate.update(isql);
        }
    }
}